﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.IO;
using System.Data.OleDb;


using Srvtools;
using System.Text;

using System.Reflection;
using System.Diagnostics;

public partial class EmployeesSalary_EmpSalaryList : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;
    bool firstload = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializeComponent(false);

            Master.DataSource = WMaster;

            SalaryDiv.Visible = false;
            ((HiddenField)this.Page.Master.FindControl("HoverId")).Value = "EmpSalary";
            ((HiddenField)EmpSalaryBar.FindControl("HoverId")).Value = "EmpSalaryList";

            SelYear.SelectedValue = DateTime.Now.Year.ToString();
            SelMonth.SelectedValue = DateTime.Now.Day > 8 ? DateTime.Now.Month.ToString() : Convert.ToString(DateTime.Now.Month - 1);


            MasterPage_MasterPage_EmpDetail master = (MasterPage_MasterPage_EmpDetail)this.Page.Master;

            master.HelpHref = "";
            master.PageName = "员工薪资单";
            master.PicUrl = "";
            master.IntiBar();

            firstload = true;
        }
    }

    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EmployeesSalary_EmpSalaryList));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = false;
        this.WMaster.PacketRecords = 400;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SEmployees.ViewEmpSalaryList";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();
    }


    protected void Search_Click(object sender, EventArgs e)
    {


        if (Request["action"] != null)
        {
            string eid = Request["eid"].ToString();

            string Year = SelYear.SelectedValue;

            if (SelMonth.SelectedValue == "0")
            {
                InitializeComponent(true);

                WMaster.SetWhere("EmpSalary.EmployeeId=" + eid + " and Years=" + Year);

                Master.DataSource = WMaster;

                SalaryDiv.Visible = true;

                WFV.Visible = false;
                wgvMaster.Visible = true;
            }
            else
            {
                InitializeComponent(true);

                WMaster.SetWhere("EmpSalary.EmployeeId=" + eid + " and Years=" + Year + " and Months=" + SelMonth.SelectedValue);

                Master.DataSource = WMaster;

                SalaryDiv.Visible = true;

                wgvMaster.Visible = false;

                WFV.Visible = true;
            }
        }
        else
        {
            HrSearchClick(false, "0");
        }

    }

    private void HrSearchClick(bool detail, string EmpSalaryId)
    {
        InitializeComponent(true);



        if (detail)
        {
            WMaster.SetWhere("EmpSalary.EmpSalaryId=" + EmpSalaryId);



            SalaryDiv.Visible = true;

            Master.DataSource = WMaster;

            SalaryDiv.Visible = true;

            wgvMaster.Visible = false;
            WFV.Visible = true;

        }
        else
        {
            string dpt = Depart.SelectDepmanetId;
            if (dpt != "0")
            {
                dpt = " And Employees.DepartmentId like '" + dpt + "%' ";
            }
            else
            {
                dpt = "";
            }
            string name = SelUser.EmpName;
            if (name != "")
            {
                name = " And Employees.EmpName like '%" + name + "%' ";
            }

            string framework = "";
            if (wddlFramework.SelectedValue != "-1")
            {
                framework = " And Employees.Framework=" + wddlFramework.SelectedValue;
            }
            else
            {
                framework = "";
            }

            string months = SelMonth.SelectedValue;
            if (months != "0")
            {
                months = " and Months=" + SelMonth.SelectedValue;
            }
            else
            {
                months = "";
            }
            WMaster.SetWhere(" 1=1" + dpt + name + framework + " and Years=" + SelYear.SelectedValue + months);

            Master.DataSource = WMaster;


            SalaryDiv.Visible = true;

            WFV.Visible = false;
            wgvMaster.Visible = true;
        }


    }

    protected void wgvMaster_DataBound(object sender, EventArgs e)
    {

        object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "EmpSalaryNoShowField", new object[] { "list" });
        if (ret[1] != null)
        {
            DataTable dt = ((DataSet)ret[1]).Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 5; j < wgvMaster.Columns.Count; j++)
                {
                    if (wgvMaster.Columns[j].HeaderText == dt.Rows[i]["EmpSalaryFieldRemark"].ToString())
                    {
                        wgvMaster.Columns[j].Visible = false;
                        break;
                    }
                }
            }

        }


        if (Master.InnerDataSet.Tables[0].Rows.Count > 0)
        {
            if (Convert.ToInt32(Master.InnerDataSet.Tables[0].Rows[0]["EmpSalaryId"]) > 0)
            {
                if (firstload)
                {
                    output.Visible = false;
                }
                else
                {
                    output.Visible = true;
                }
            }
            else
            {
                output.Visible = false;
            }
        }
        else
        {
            output.Visible = false;
        }


        if (Request["action"] != null)
        {
            hrSearch.Visible = false;

        }
        else
        {
            EmpSalaryBar.Visible = false;
        }
    }


    protected void WFV_DataBound(object sender, EventArgs e)
    {
        if (WFV.CurrentMode == FormViewMode.ReadOnly)
        {
            object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "EmpSalaryNoShowField", new object[] { "detail" });
            if (ret[1] != null)
            {
                DataTable dt = ((DataSet)ret[1]).Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object lbl = WFV.FindControl("lbl" + dt.Rows[i]["EmpSalaryFieldName"].ToString());
                    object fd = WFV.FindControl("fd" + dt.Rows[i]["EmpSalaryFieldName"].ToString());
                    if (lbl != null)
                    {
                        ((System.Web.UI.HtmlControls.HtmlTableCell)lbl).Visible = false;
                    }
                    if (fd != null)
                    {
                        ((System.Web.UI.HtmlControls.HtmlTableCell)fd).Visible = false;
                    }
                }
            }

            if (Master.InnerDataSet.Tables[0].Rows.Count > 0)
            {
                if (Convert.ToInt32(Master.InnerDataSet.Tables[0].Rows[0]["EmpSalaryId"]) > 0)
                {
                    output.Visible = true;
                }
                else
                {
                    output.Visible = false;
                }
            }
            else
            {
                output.Visible = false;
            }
        }
    }
    protected void wgvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;

            ImageButton imb = (ImageButton)e.Row.FindControl("imb");

            imb.CommandArgument = drv["years"].ToString() + "," + drv["months"].ToString() + "," + drv["EmpSalaryId"].ToString();


        }
    }
    protected void wgvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "show")
        {
            if (e.CommandArgument.ToString() != "")
            {
                string[] t = e.CommandArgument.ToString().Split(',');
                SelYear.SelectedValue = t[0];
                SelMonth.SelectedValue = t[1];
                if (Request["action"] != null)
                {
                    Search_Click(null, null);
                }
                else
                {
                    HrSearchClick(true, t[2]);
                }
            }
        }
    }
    protected void wddlFramework_DataBound(object sender, EventArgs e)
    {
        wddlFramework.Items.Remove(new ListItem("—全部—", "-1"));

        wddlFramework.Items.Add(new ListItem("—全部—", "-1"));

        wddlFramework.SelectedValue = "-1";
    }

    protected void DownLoadSalaryList_Click(object sender, EventArgs e)
    {


        object obj = Master.InnerDataSet;

        string path = MapPath("~/UploadFile/Salary/Template/");


        DataTable dt = ((DataSet)obj).Tables[0];


        if (dt.Rows.Count <= 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('没有记录')", true);
            return;
        }

        dt.Columns.Remove("EditEmpId");
        dt.Columns.Remove("EditTime");
        dt.Columns.Remove("Framework");

        dt.Columns["EmpSalaryId"].ColumnName = "薪资编号";
        dt.Columns["EmployeeId"].ColumnName = "员工编号";
        dt.Columns["Years"].ColumnName = "年份";
        dt.Columns["Months"].ColumnName = "月份";
        dt.Columns["EmpName"].ColumnName = "员工姓名";
        dt.Columns["DepartmentName"].ColumnName = "部门";
        dt.Columns["ValueName"].ColumnName = "员工类别";

        dt.Columns["BZYX"].ColumnName = "标准月薪";
        dt.Columns["GWJT"].ColumnName = "岗位津贴";
        dt.Columns["GLBT"].ColumnName = "各类补贴";

        dt.Columns["JJGZ1"].ColumnName = "计件工资1";
        dt.Columns["FJJSR"].ColumnName = "非计件收入";
        dt.Columns["BFGZ"].ColumnName = "补发工资";
        dt.Columns["JBXS"].ColumnName = "加班小时";
        dt.Columns["JBGZ"].ColumnName = "加班工资";
        dt.Columns["ZBTS"].ColumnName = "中班天数";
        dt.Columns["ZBBT"].ColumnName = "中班补贴";
        dt.Columns["JBF"].ColumnName = "加班费";
        dt.Columns["JJ"].ColumnName = "奖金";
        dt.Columns["CQFWJ"].ColumnName = "长期服务奖";

        dt.Columns["LZBCJ"].ColumnName = "离职补偿金";
        dt.Columns["YFHJ"].ColumnName = "应发合计";

        dt.Columns["SJYFS"].ColumnName = "实际应发数";

        dt.Columns["BJXS"].ColumnName = "病假小时";
        dt.Columns["BJKK1"].ColumnName = "病假扣款1";
        dt.Columns["BJKK2"].ColumnName = "病假扣款2";
        dt.Columns["SJXS"].ColumnName = "事假小时";
        dt.Columns["SJKK1"].ColumnName = "事假扣款1";
        dt.Columns["SJKK2"].ColumnName = "事假扣款2";
        dt.Columns["YLJ"].ColumnName = "养老金";
        dt.Columns["YBJ"].ColumnName = "医保金";

        dt.Columns["DYJ"].ColumnName = "待业金";
        dt.Columns["GJJ"].ColumnName = "公积金";
        dt.Columns["ZLKK"].ColumnName = "质量扣款";
        dt.Columns["QTKK"].ColumnName = "其他扣款";
        dt.Columns["DKS"].ColumnName = "代扣税";
        dt.Columns["KKHJ"].ColumnName = "扣款合计";
        dt.Columns["JLKK"].ColumnName = "纪律扣款";
        dt.Columns["SFHJ"].ColumnName = "实发合计";
        dt.Columns["YSGZ"].ColumnName = "应税工资";
        dt.Columns["SJJJJS"].ColumnName = "四金计缴基数";

        dt.Columns["YX"].ColumnName = "月薪";
        dt.Columns["SJBZ"].ColumnName = "四金标志";
        dt.Columns["NZJ"].ColumnName = "年终奖";
        dt.Columns["NZJDKS"].ColumnName = "年终奖代扣税";
        dt.Columns["GZDKS"].ColumnName = "工资代扣税";
        dt.Columns["KSHJ"].ColumnName = "扣税合计";

        DataTable dtt = dt.Clone();
        dtt.Merge(dt);
        Srvtools.InfoDataSet ids = new Srvtools.InfoDataSet();
        ids.RemoteName = "GLModule.cmdColDEF";
        ids.RealDataSet.Tables.Add(dtt);

        ids.ToExcel(0, path + "salarylistoutput.xls");

        /*
        return;
        ExcelHelper ex = new ExcelHelper();
        ex.DataTableToExcel(dt, 1, 1);
        ex.OutputFilePath = path + "salarylistoutput.xls";
        ex.OutputExcelFile();
        */

        FileInfo fn = new System.IO.FileInfo(path + "salarylistoutput.xls");

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn.Name);
        HttpContext.Current.Response.WriteFile(fn.FullName);
        HttpContext.Current.Response.End();
    }
}
